package com.securitytrace.client;

import java.io.IOException;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.apache.log4j.Logger;

import com.securitytrace.client.cache.UserCache;
import com.securitytrace.client.rfid.driver.Reader;
import com.securitytrace.client.rfid.driver.console.LineAddress;
import com.securitytrace.client.ui.MainFrame;
import com.securitytrace.client.ui.MainFrameFactory;
import com.securitytrace.client.util.DateUtil;



public class ReaderObserver implements Observer{

	private static final Logger logger = Logger.getLogger(ReaderObserver.class);
	
	private MainFrame mainFrame = MainFrameFactory.getMainFrame();
	
	@Override
	public void update(Observable o, Object arg) {

		logger.debug("观察到 读取了RFID信息");
		
		Reader reader = (Reader) arg;
	
		String currentAccount = UserCache.accountMap.get("account");
		long roleId = UserCache.userMap.get(currentAccount).getRole().getId();
		
		if(1 != roleId){
			String readStr = null;
			try {
				readStr = reader.read(new LineAddress(1));
			} catch (IOException e) {
				e.printStackTrace();
			}
			mainFrame.getProductOtherInfoTextArea().setText(readStr);
		}
		String frontText = mainFrame.getOptionTextArea().getText();
		mainFrame.getOptionTextArea().setText(frontText+"登记成功! "+DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"\n");
	}
}
